package cn.swu.Waimai.ui.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import cn.swu.Waimai.WaimaiApplication
import cn.swu.Waimai.R
import cn.swu.Waimai.center.LoginData
import cn.swu.Waimai.ui.activity.*
import cn.swu.Waimai.ui.viewModel.UserViewModel
import kotlinx.android.synthetic.main.activity_me.*


class MeFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.activity_me, container, false)
    }

    @SuppressLint("WrongConstant", "UseRequireInsteadOfGet")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val viewModel by lazy { ViewModelProvider(this).get(UserViewModel::class.java) }

        if (LoginData.LOGIN_STATUS == 0){
            login_out.visibility = View.GONE

        }
        //我的地址
        myaddress.setOnClickListener {
            if (LoginData.LOGIN_STATUS == 1) {
                val intent = Intent(WaimaiApplication.context, MyAddressActivity::class.java)

                startActivity(intent)
            } else {
                val intent = Intent(WaimaiApplication.context, LoginActivity::class.java)
                startActivity(intent)
            }
        }
        //订单
        myorder.setOnClickListener{
            val intent = Intent(WaimaiApplication.context, MyOrderActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
            startActivity(intent)
        }

        login_out.setOnClickListener{
            val pers = activity?.getSharedPreferences("userInfo", Context.MODE_PRIVATE)
            pers?.edit()?.clear()?.apply()
            LoginData.LOGIN_STATUS = 0
            LoginData.GET_ADDRESS = 0
            val intent = Intent(WaimaiApplication.context, LoginActivity::class.java)
            startActivity(intent)
        }
    }
}